package com.myk.game.gobangchess.rooms.oldrooms;

import com.myk.game.gobangchess.Room;
import com.myk.game.gobangchess.constants.BoardStyleData;
import com.myk.game.gobangchess.constants.WinMode;
import com.myk.game.gobangchess.utils.GridsUtils;
import com.myk.game.gobangchess.signs.AroundSign;

import java.util.ArrayList;
import java.util.List;

/**
 * 禁点五子棋
 */

public class GoBangVariant1Room extends Room {
    public GoBangVariant1Room(String roomName) {
        super(roomName, "\t\t五子棋变体", "禁点五子棋", BoardStyleData.getGoBangStyle(19, 19), true, true);
    }

    @Override
    public String getRuleText() {
        return "一方选3个禁点\n" +
                "另一方选执黑白）\n" +
                "黑一天元\n"+
                "五子连珠胜\n"+
                "终盘时占据场格多者胜";
    }
    @Override
    protected void onStartPrepare() {
        gameTip = "落三步禁点,不可天元";
        curGrids[9][9]=1;
        noticeBlackMove();


    }

    @Override
    public void doRule(int row, int col, int value) {
        if (step<=2){

            if (curGrids[row][col] != 0) {return;
            }else{
                curGrids[row][col] = 9;
                stepAdd();
                if (step==3){
                    gameTip = "点天元为黑方，点其他格为白方";
                    noticeWhiteMove();
                }else{noticeBlackMove();}
                return;
            }
        }
        if (step==3){

            if (row==9&&col==9){
                curGrids[9][9]=2;
                gameTip ="-1";
                stepAdd();
                noticeBlackMove();
                return;
            }else if(curGrids[row][col] == 0){
                curGrids[9][9]=1;
                curGrids[row][col]=2;
                gameTip ="-1";
                stepAdd();
                noticeBlackMove();
                return;
            }else{return;}
        }
        if (curGrids[row][col] == 0) { //只能落在空位置

            signList.clear();
            List<Integer> ids = new ArrayList<>();
            ids.add(getIndex(row, col));
            signList.add(new AroundSign(ids, "#00000000", "#6600FF00", 1.2f));
            curGrids[row][col] = value; //实际落子
            stepAdd();
            ; //步数统计加增加

            //黑方落子时
            if (value == 1) {
                if (checkFormation(1) > 0) {//黑方连5成功
                    doOver(WinMode.BLACK_WIN, player_black.getNickNameOrTemp() + "执黑棋战胜" + player_white.getNickNameOrTemp());
                } else {
                    int blankNum = GridsUtils.getDataCount(curGrids, 0);
                    if (blankNum == 0) {//没有胜负也没有空子了算战平
                        doOver(WinMode.BOTH_DRAW, player_black.getNickNameOrTemp() + "-黑棋战平" + player_white.getNickNameOrTemp() + "-白棋");
                    } else {
                        noticeWhiteMove();
                    }
                }
            }

            //白方落子时
            else if (value == 2) {
                if (checkFormation(2) > 0) {
                    doOver(WinMode.WHITE_WIN, player_white.getNickNameOrTemp() + "执白棋战胜" + player_black.getNickNameOrTemp());
                } else {
                    int blankNum = GridsUtils.getDataCount(curGrids, 0);  //没有胜负也没有空子了算战平
                    if (blankNum == 0) {
                        doOver(WinMode.BOTH_DRAW, player_black.getNickNameOrTemp() + "-黑棋战平" + player_white.getNickNameOrTemp() + "-白棋");
                    } else {
                        noticeBlackMove();
                    }
                }
            }

        }
    }

    //获取成5的次数
    private int checkFormation(int value) {
        int rows = curGrids.length;
        int cols = curGrids[0].length;
        int formationCount = 0;

        // 水平方向
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols - 4; j++) {
                int length = 0;
                for (int k = j; k < j + 5; k++) {
                    if (curGrids[i][k] == value) {
                        length++;
                    } else {
                        break;
                    }
                }
                if (length >= 5) {
                    formationCount += (length - 4);
                }
            }
        }

        // 垂直方向
        for (int i = 0; i < rows - 4; i++) {
            for (int j = 0; j < cols; j++) {
                int length = 0;
                for (int k = i; k < i + 5; k++) {
                    if (curGrids[k][j] == value) {
                        length++;
                    } else {
                        break;
                    }
                }
                if (length >= 5) {
                    formationCount += (length - 4);
                }
            }
        }

        // 左上到右下方向
        for (int i = 0; i < rows - 4; i++) {
            for (int j = 0; j < cols - 4; j++) {
                int length = 0;
                for (int k = 0; k < 5; k++) {
                    if (curGrids[i + k][j + k] == value) {
                        length++;
                    } else {
                        break;
                    }
                }
                if (length >= 5) {
                    formationCount += (length - 4);
                }
            }
        }

        // 右上到左下方向
        for (int i = 0; i < rows - 4; i++) {
            for (int j = 4; j < cols; j++) {
                int length = 0;
                for (int k = 0; k < 5; k++) {
                    if (curGrids[i + k][j - k] == value) {
                        length++;
                    } else {
                        break;
                    }
                }
                if (length >= 5) {
                    formationCount += (length - 4);
                }
            }
        }

        return formationCount;
    }

}
